package movee.auth.jwt.config;


import movee.auth.jwt.security.SecurityFilter;
import movee.auth.jwt.security.SecurityService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;

/**
 * 配置
 *
 * @author movee
 * */
@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Value("${security.jwt.secret}")
    private String secret;

    @Value("${spring.application.name}")
    private String serviceName;

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.csrf().disable()
                .authorizeRequests()
                // 这些端点不需要认证
                .antMatchers("/actuator/health", "/actuator/info").permitAll()
                // 其他的只要认证就可以
                .anyRequest().authenticated()
                .and()
                // 增加filter
                .addFilterBefore(new SecurityFilter(new SecurityService(secret, serviceName)),
                        UsernamePasswordAuthenticationFilter.class);

    }


}
